package FillMythMetadataTMDB;
use FillMythMetadataUtilities;
use strict;


#this API key can be obtained by asking it from themoviedb.org admin.
#do not use this API key for other scripts!
my $themoviedb_API_key = "79302e9ad1a5d71e8d62a82334cdbda4";

my $cached_tmdb_movie_id = "";
my $cached_imdb_movie_name = "";
my $cached_imdb_movie_id = "";
my ($cached_imdb_title_url, $cached_imdb_title, $cached_imdb_plot, $cached_imdb_director, $cached_imdb_release_date, $cached_imdb_rating, $cached_imdb_poster, $cached_imdb_fanart, $cached_motechnet_poster, $cached_content_rating);

sub get_movie_meta_data_from_tmdb
{
	#TMDB added by Mark Nolan
	my $imdb_id = $_[0];
	my $tmdb_id= $_[1];
	
	if ((!$tmdb_id) || length($tmdb_id) eq 0)
	{
		#Get TMDB_ID
		my $url = "http://api.themoviedb.org/2.1/Movie.imdbLookup/en/xml/".$themoviedb_API_key."/tt".$imdb_id;
		my $id_page = FillMythMetadataHttpCaller::get_http_response($url);
		
		if ($id_page =~ m/<id>(.*?)<\/id>/i)
		{
			$tmdb_id = $1;
		}
	}
	
	if (($tmdb_id ne $cached_tmdb_movie_id))
	{
		
		my $cached_tmdb_title_url = "http://api.themoviedb.org/2.1/Movie.getInfo/en/xml/".$themoviedb_API_key."/".$tmdb_id;
		my $movie_page = FillMythMetadataHttpCaller::get_http_response($cached_tmdb_title_url);

		($cached_imdb_title) = $movie_page =~ m/<name>(.*?)<\/name>/i;
		
		($cached_imdb_poster) = $movie_page =~ m/<image type=\"poster\" url=\"([^\"]+?)\" size=\"original\"/i;
		if (!$cached_imdb_poster) { $cached_imdb_poster = ""; }

		($cached_imdb_fanart) = $movie_page =~ m/<image type="backdrop" url=\"([^\"]+?)\" size="original"/i;
		if (!$cached_imdb_fanart) { $cached_imdb_fanart = ""; }

		($cached_imdb_rating) = $movie_page =~ m/<rating>(.*?)<\/rating>/i;
		if (!$cached_imdb_rating) { $cached_imdb_rating = 0; }
		
		($cached_imdb_director) = $movie_page =~ m/<person.*?name="(.*?)".*?job="Director".*?\/>/i;
		if (!$cached_imdb_director) {$cached_imdb_director = "";}
		
		($cached_imdb_release_date) = $movie_page =~ m/<released>(\d{4}).*?</;
		if (!$cached_imdb_release_date) {$cached_imdb_release_date = "1900";}
		
		($cached_imdb_plot) = $movie_page =~ m/<overview>(.*?)<\/overview>/i;
		if (!$cached_imdb_plot) {$cached_imdb_plot = "-";}
		
		if (!$cached_content_rating) {$cached_content_rating = "NR";}
		
		print "title: '$cached_imdb_title'\n";
		print "poster: '$cached_imdb_poster'\n";
		print "rating: '$cached_imdb_rating'\n";
		print "director: '$cached_imdb_director'\n";
		print "release_date: '$cached_imdb_release_date'\n";
		print "plot: '$cached_imdb_plot'\n";
		$cached_imdb_movie_id = $imdb_id;
		my $cached_imdb_movie_id = $tmdb_id;
	}
	
	return ($cached_imdb_movie_id, $cached_imdb_title_url, $cached_imdb_title, $cached_imdb_plot, $cached_imdb_director, $cached_imdb_release_date, $cached_imdb_poster, $cached_imdb_fanart, $cached_imdb_rating, $cached_motechnet_poster, $cached_content_rating);
}

my $cached_tmdb_ID = "";
my $cached_imdb_ID = "";
sub get_imdb_ID_for_movie_name_tmdb
{
	my $movie_name = $_[0];
	my $releaseYear = $_[1];
	my $imdb_ID = "";
	my $tmdb_ID = "";
	
	if ($movie_name ne $cached_imdb_movie_name)
	{
		$cached_imdb_movie_name = $movie_name;
		#http://api.themoviedb.org/2.0/Movie.search?title=Transformers&api_key=79302e9ad1a5d71e8d62a82334cdbda4
		#http://api.themoviedb.org/2.1/Movie.search/en/xml/79302e9ad1a5d71e8d62a82334cdbda4/Transformers
		my $tmdb_response = FillMythMetadataHttpCaller::get_http_response("http://api.themoviedb.org/2.1/Movie.search/en/xml/".$themoviedb_API_key."/".$movie_name);
		
		my $best_similarity = 100000000;
		while ($tmdb_response =~ m/<movie>(.+?)<\/movie>/gi)
		{
			my $movie = $1;
			
			if ($movie eq "Your query didn't return any results.")
			{
				print "Can not locate movie '$movie_name' in themoviedb.\n";
				return ("", "");
			}
			
			my $possible_name = "";
			($possible_name) = $movie =~ m/<name>(.*?)<\/name>/i;
			my $possible_name_alt = "";
                        ($possible_name_alt) = $movie =~ m/<alternative_name>(.*?)<\/alternative_name>/i;

			my $possible_tmdb = "";
			($possible_tmdb) = $movie =~ m/<id>(.*?)<\/id>/i;
			my $possible_imdb = "";
			($possible_imdb) = $movie =~ m/<imdb_id>tt(.*?)<\/imdb_id>/i;
			my $possible_year = "";
			($possible_year) = $movie =~ m/<released>(.+?)-(.+?)-(.+?)<\/released>/i;
			
			
			my $current_similarity = FillMythMetadataUtilities::levenshtein($movie_name, $possible_name);
			my $alt_current_similarity = FillMythMetadataUtilities::levenshtein($movie_name, $possible_name_alt);
			if ($releaseYear)
			{#Check Release Date
				if ($releaseYear eq $possible_year)
				{
					#a little bonus
					$current_similarity = $current_similarity*0.8;
					$alt_current_similarity = $alt_current_similarity*0.8;
				}
			}
			
			if (($current_similarity < $best_similarity) || ($alt_current_similarity < $best_similarity))
			{
				if ($tmdb_ID eq "")
				{
					print "Found a possible match for '$movie_name' as '$possible_name' (ID $possible_tmdb)\n";
				}
				else
				{
					print "Found a better possible match for '$movie_name' as '$possible_name' (ID $possible_tmdb)\n";
				}
				$best_similarity = $current_similarity;
				if ($alt_current_similarity < $best_similarity)
				{
					$best_similarity = $alt_current_similarity;
				}
				$imdb_ID = $possible_imdb;
				$tmdb_ID = $possible_tmdb;

			}
		}
		if ($tmdb_ID)
		{
			print "Found ID '$tmdb_ID' for movie '$movie_name' at themoviedb.\n";
			$cached_imdb_ID = $imdb_ID;
			$cached_tmdb_ID = $tmdb_ID;
			return ($cached_imdb_ID, $cached_tmdb_ID);
		}
		else
		{
			print "Can not locate movie '$movie_name' in themoviedb.\n";
			return ("", "");
		}
	
	}
	
	return ($cached_imdb_ID, $cached_tmdb_ID);
}

1;
